package com.aster.service.manage.service.impl;

import com.aster.common.base.BaseServiceImpl;
import com.aster.common.dto.UserToken;
import com.aster.common.exception.ServiceException;
import com.aster.common.utils.PageUtils;
import com.aster.service.manage.dao.CommissionRatioInfoDao;
import com.aster.service.manage.dao.EnterpriseProfitRatioInfoDao;
import com.aster.service.manage.dao.EscortEnterpriseInfoDao;
import com.aster.service.manage.dao.EscortUserInfoDao;
import com.aster.service.manage.domain.TbCommissionRatioInfo;
import com.aster.service.manage.domain.TbEnterpriseProfitRatioInfo;
import com.aster.service.manage.domain.TbEscortEnterpriseInfo;
import com.aster.service.manage.domain.TbEscortUserInfo;
import com.aster.service.manage.dto.DtoEscortEnterpriseInfo;
import com.aster.service.manage.dto.DtoEscortUserInfo;
import com.aster.service.manage.dto.do2dto.EscortEnterpriseInfoConvert;
import com.aster.service.manage.dto.do2dto.EscortUserInfoConvert;
import com.aster.service.manage.service.CommissionRatioInfoService;
import com.aster.service.manage.service.EscortUserInfoService;
import com.aster.service.manage.service.criteria.EscortUserInfoCriteria;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * <br>
 * <b>功能：</b>TbEscortUserInfoService<br>
 */
@Service("tbEscortUserInfoService")
public class  EscortUserInfoServiceImpl  extends BaseServiceImpl implements EscortUserInfoService {
  private final static Logger log= Logger.getLogger(EscortUserInfoServiceImpl.class);
	

	@Autowired
    private EscortUserInfoDao dao;
	@Autowired
	private CommissionRatioInfoService commissionRatioInfoService;
    @Autowired
    private EscortEnterpriseInfoDao escortEnterpriseInfoDao;

		
	public EscortUserInfoDao getDao() {
		return dao;
	}


	@Override
	public PageUtils getEscortUserInfoForPage(EscortUserInfoCriteria criteria) {
		Integer count = dao.queryByCount(criteria);
		if(count == null || count == 0){
			return new PageUtils(null,0);
		}
		List<TbEscortUserInfo> list = dao.queryByList(criteria);
		List<DtoEscortUserInfo> dtoList = EscortUserInfoConvert.MAPPER.dos2dtos(list);
		List<DtoEscortUserInfo> dtoEscortEnterpriseInfos = new ArrayList<>();
		// 循环取出陪护员佣金
		for (DtoEscortUserInfo dtoEscortUserInfo:dtoList) {
			Integer escortUserId = dtoEscortUserInfo.getId();
			TbCommissionRatioInfo info = new TbCommissionRatioInfo();
			info.setState(0);
			info.setEscortUserId(escortUserId);
			info = commissionRatioInfoService.queryByEscortUserIdAndState(info);
			dtoEscortUserInfo.setCommissionRatio(info.getCommissionRatio());

            // 根据企业ID取出企业名称
            TbEscortEnterpriseInfo escortEnterpriseInfo = new TbEscortEnterpriseInfo();
            escortEnterpriseInfo.setId(dtoEscortUserInfo.getEnterpriseId());
            escortEnterpriseInfo = escortEnterpriseInfoDao.selectByOne(escortEnterpriseInfo);
            if (escortEnterpriseInfo  != null){
				dtoEscortUserInfo.setEnterpriseName(escortEnterpriseInfo.getName());
			}
            dtoEscortEnterpriseInfos.add(dtoEscortUserInfo);
        }
		return new PageUtils(dtoEscortEnterpriseInfos,count);
	}

	@Override
	public void addEscortUserInfo(TbEscortUserInfo tbEscortUserInfo, UserToken userToken) throws ServiceException {
		if (tbEscortUserInfo == null){
			throw new ServiceException(10001,"参数为空");
		}
		try {
			// 所属企业ID
			int enterpriseId = userToken.getEnterpriseId();
			tbEscortUserInfo.setEnterpriseId(enterpriseId);
			tbEscortUserInfo.setCreateTime(new Date());
			tbEscortUserInfo.setUpdateTime(new Date());
			tbEscortUserInfo.setCreateUserId(Integer.parseInt(userToken.getUserId()));
			tbEscortUserInfo.setUpdateUserId(Integer.parseInt(userToken.getUserId()));
			tbEscortUserInfo.setState(1);
			tbEscortUserInfo.setDeleted(1);
			dao.add(tbEscortUserInfo);
		}catch (Exception ex){
			throw new ServiceException(10000,ex.getMessage());
		}
	}

	@Override
	public void editEscortUserInfo(TbEscortUserInfo tbEscortUserInfo, UserToken userToken) throws ServiceException {
		if (tbEscortUserInfo == null){
			throw new ServiceException(10001,"参数为空");
		}
		try{
			tbEscortUserInfo.setUpdateUserId(Integer.parseInt(userToken.getUserId()));
			tbEscortUserInfo.setUpdateTime(new Date());
			dao.updateBySelective(tbEscortUserInfo);
		}catch (Exception ex){
			ex.printStackTrace();
			throw new ServiceException(10000,ex.getMessage());
		}
	}

	@Override
	public TbEscortUserInfo selectByOne(Object id) {
		return dao.selectByOne(id);
	}
}
